<script setup lang="ts">
import { ref, toRefs } from "vue";
import { onShow, onHide, onLoad } from "@dcloudio/uni-app";
import _ from "lodash";
import assets from "@/utils/assets";
import { previewImage } from "@/utils/commonUtils";
import { upload } from "@/service/service";
// import ImgView from "@/components/ImgView/index.vue";

let props = defineProps(["title", "amount", "imgs", "args", "light"]);
let { title, amount, imgs, args, light } = toRefs(props);

let emit = defineEmits(["upload", "change"]);

let chooseImage = () => {
  //选择图片
  uni.chooseImage({
    count: amount?.value || 1,
    sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
    sourceType: ["album", "camera"], //从相册选择
    success: (res) => {
      upload(res.tempFilePaths).then((file: any) => {
        emit(
          "upload",
          _.map(file, (item) => {
            return _.get(item, "path");
          }),
          args?.value
        );
      });
    },
  });
};

let currImg = ref("");
let handleViewImg = (img: any) => {
  currImg.value = img;
  previewImage({
    imgList: imgs?.value || [],
    url: img,
  });
};

let handleCloseImg = () => {
  currImg.value = "";
};

let handleDeleteImg = (imgIndex: any, currImgs: any[]) => {
  let arr = [...currImgs];
  arr.splice(imgIndex, 1);
  emit("change", arr, args?.value);
};
</script>

<template>
  <div class="form-item">
    <div class="form-title">
      <span :class="light ? 'title name-light' : 'title'">{{ title }}</span>
      <span class="amount">
        {{ _.size(imgs) }}
        /
        {{ amount }}</span
      >
    </div>
    <div class="img-wrap">
      <div
        v-for="(img, imgIndex) in imgs"
        class="img-item-wrap"
        :key="imgIndex"
      >
        <image
          :src="img"
          class="img-item"
          mode="aspectFit"
          @click.stop="handleViewImg(img)"
        />
        <span class="del-img-btn" @click="handleDeleteImg(imgIndex, imgs)">
          <img :src="assets.delete_img_btn" class="delete_img_btn" />
        </span>
      </div>
      <img :src="assets.upload_img" class="upload_img" @click="chooseImage" />
    </div>
  </div>
  <!-- <ImgView :src="currImg" @close="handleCloseImg" /> -->
</template>

<style lang="less" scoped>
.form-item {
  display: flex;
  flex-direction: column;
}
.form-title {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  padding: 30rpx 34rpx;
}
.title {
  font-family: PingFangSC, PingFang SC;
  font-weight: bold;
  font-size: 32rpx;
  color: #333333;
}
.name-light {
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 32rpx;
  color: #333333;
}
.amount {
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 30rpx;
  color: #333333;
}
.upload_img {
  width: 168rpx;
  height: 168rpx;
}
.img-wrap {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  flex-wrap: wrap;
  padding: 0 34rpx;
  padding-bottom: 34rpx;
  gap: 16rpx;
}

.img-item-wrap {
  width: 168rpx;
  height: 168rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12rpx;
  border: 1rpx solid #e6e6e6;
  box-sizing: border-box;
  position: relative;
}
.del-img-btn {
  position: absolute;
  top: -10rpx;
  right: -10rpx;
  padding: 6rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}
.delete_img_btn {
  width: 40rpx;
  height: 40rpx;
}
.img-item {
  width: 100%;
  height: 100%;
}
</style>
